/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xui_lib.component.imageview.edit;

import ohos.agp.components.AttrSet;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import static com.xuexiang.xui_lib.component.imageview.edit.PhotoFilter.NONE;

/**
 * 滤镜处理
 *
 * @author xuexiang
 * @since 2019-10-28 10:01
 */
class ImageFilterView extends SurfaceProvider {
    private static final String TAG = "ImageFilterView";
    private int[] mTextures = new int[2];
    private TextureRenderer mTexRenderer = new TextureRenderer();
    //private boolean mInitialized = false;
    private PhotoFilter mCurrentEffect;
    private PixelMap mSourceBitmap;
    private CustomEffect mCustomEffect;
    private OnBitmapSaveListener mOnBitmapSaveListener;
    //private boolean isSaveImage = false;

    public ImageFilterView(Context context) {
        this(context, null);
    }

    public ImageFilterView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        //init();
    }

    void setFilterEffect(PhotoFilter effect) {
        mCurrentEffect = effect;
        mCustomEffect = null;
    }

    void setFilterEffect(CustomEffect customEffect) {
        mCustomEffect = customEffect;
    }

    void setSourceBitmap(PixelMap sourceBitmap) {
        mSourceBitmap = sourceBitmap;
        //mInitialized = false;
    }

    void saveBitmap(OnBitmapSaveListener onBitmapSaveListener) {
        mOnBitmapSaveListener = onBitmapSaveListener;
        //isSaveImage = true;
        if(mSourceBitmap!=null){
            mOnBitmapSaveListener.onBitmapReady(mSourceBitmap);
        }
    }

    private void renderResult() {
        if (mCurrentEffect != NONE || mCustomEffect != null) {
            // if no effect is chosen, just render the original bitmap
            mTexRenderer.renderTexture(mTextures[1]);
        } else {
            // render the result of applyEffect()
            mTexRenderer.renderTexture(mTextures[0]);
        }
    }
}
